# -*- coding: utf-8 -*-
# @Time     : 2023/12/8 0:04
# @Author   : JustFly
# @File     : update_access_token.py
# @Software : PyCharm

import requests
from config.bot import APP_ID, CLIENT_SECRET, ACCESS_TOKEN_FILE
from botpy import logger
from common.scripts import get_datetime

# 获取凭证
def func():
    url = 'https://bots.qq.com/app/getAppAccessToken'
    headers = {'Content-Type': 'application/json'}

    data = {
        "appId": APP_ID,
        "clientSecret": CLIENT_SECRET
    }

    response = requests.post(url, json=data, headers=headers)

    if response.status_code == 200:
        logger.info(f"【{get_datetime.func()}】更新access_token成功 ！{response.json()['access_token']}")
        save_token(response.json()['access_token'])
        return True
    else:
        return False

def save_token(access_token):
    # 打开文件，如果文件不存在则创建它，如果存在则在末尾添加内容
    with open(ACCESS_TOKEN_FILE, 'w') as f:
        f.write(access_token)


if __name__ == '__main__':
    func()